<?php
class Feedback_Model_Item extends Zen_List {
	public function __construct() {
		$this->table = new Zend_Db_Table('feedback');
	}
	
	public function create($params) {
		$form = new Feedback_Form_Contact ();
		$fields = $form->getElements ();
		$text = "";
		
		foreach ( $fields as $field ){
		 	if ($field->getType()!="Zend_Form_Element_Submit"){
				$name = $field->getName();
				$text .= "<b>";
				$text .= $field->getLabel()." ";
				$text .= "</b>";
				if (isset($params[$name])){
					$data = "";
					if(is_array($params [$name])){
						foreach ( $params [$name] as $value ){
							$data .= $value . "; ";
						}
					}else{
						$data = $params [$name];
					}
					$text .= $data;
				}else{
					$text .= "-------------------";
				}
				$text .= "<br/>";
				
			}
		}
		$text .= "<br/><br/>Это сообщение отправлено, роботом. Отвечать на него не нужно. Ваш ответ отправляйте, на адрес, указанный в поле email.";
		$params ['text'] = $text;
		$res = $this->table->createRow ( $params )->save ();
		if ($res) {
			$email = new Communication_Model_Email ();
			$cnf = new Zend_Config_Ini ( ROOT_PATH . '/site/modules/feedback/settings/feedback.ini' );
			$globalConf = Zend_Registry::get ( 'cnf' );
			
			$config = $cnf->toArray ();
			if ($config ['email'] != "") {
				$emails = explode ( ',', $config ['email'] );
				foreach ( $emails as $rec ) {
					$message = array ('from' => 'feedback@' . $globalConf->domen, 'subject' => 'Новое сообщение на сайте ' . $globalConf->domen, 'text' => $text, 'to' => $rec );
					$email->create ( $message );
				}
				$email->send_messages ();
			}
		}
		
		return $res;
	}
}
?>